/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.strrchr
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
#include "gui.h"
#include <sys/stat.h>

typedef struct _WinList
{
   Evas_Object *win;
   Evas_Object *li;
} Winlist;

void clean_up(void)
{
	//unlink the fifo files
	unlink("/tmp/intone-mplayer");
	unlink("/tmp/mplayer-intone");

	//release_cpu();
}

int timer_func(void *data)
{
	char cmd[255], *tok, tystr[3], ty[3];
	int ret, hr, min, sec;
	ssize_t read = 0;
	size_t len = 0;
	char *dur = "";
	
	//get time position
	sprintf(cmd, "get_time_pos\n");
	ret = write(fd, cmd, strlen(cmd));
	//read data from mplayer output
	while(strncmp(dur, "ANS_TIME_POSITION", 17) != 0) {
		read = getline(&dur, &len, mp_data);
		if ((strncmp(dur, "EOF code: 1", 11) == 0) || 
		    (strncmp(dur, "LAVF_header: av_find_stream_info() failed", 41) == 0)) {
			//stop playing clear the screen
			return 0;	
		}
	}
	strcat(dur, "=");
	tok = strtok(dur, "=");
	tok = strtok(NULL, "=");
	//set position data
	pos_time = atoi(tok);
	hr = pos_time / 3600;
	min = pos_time / 60;
	min = min % 60;
	sec = pos_time % 60;
	if (min < 10) sprintf(ty, "0%d", min);
	else sprintf(ty, "%d", min);
	if (sec < 10) sprintf(tystr, "0%d", sec);
	else sprintf(tystr, "%d", sec);
	sprintf(cmd, "%d:%s:%s", hr, ty, tystr);
	elm_slider_value_set(pos_slider, (double)pos_time);
	elm_label_label_set(play_cur_pos, cmd);
	return 1;
}

void set_duration(void)
{
	char *dur = "", cmd[255], *tok, tystr[3];
	int ret, hr, min, sec;
	ssize_t read;
	size_t len = 0;

	//get song duration
	sprintf(cmd, "get_time_length\n");
	ret = write(fd, cmd, strlen(cmd));
	//read data from mplayer output
	while(strncmp(dur, "ANS_LENGTH", 10) != 0) {
		  read = getline(&dur, &len, mp_data);
	}
	strcat(dur, "=");
	tok = strtok(dur, "=");
	tok = strtok(NULL, "=");
	song_dur = atoi(tok);
	hr = song_dur / 3600;
	min = song_dur / 60;
	min = min % 60;
	sec = song_dur % 60;
	if (sec < 10) sprintf(tystr, "0%d", sec);
	else sprintf(tystr, "%d", sec);
	sprintf(cmd, "%d:%d:%s", hr, min, tystr);
	elm_slider_min_max_set(pos_slider, 0, song_dur);
	elm_label_label_set(play_dur, cmd);
}

void
prev_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	
}

void
next_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	
}

void
play_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
    int ret;

	if (play_state == PLAYING) {
		ret = write(fd, "pause\n", 6);
		ecore_timer_freeze(timer1);
		play_state = PAUSE;
		return;
	}
	if (play_state == PAUSE) {
		ret = write(fd, "pause\n", 6);
		ecore_timer_thaw(timer1);
		play_state = PLAYING;
	}
}

void
vol_changed(void *data, Evas_Object *obj, void *event_info)
{
	char volume[32];
	double val;
	int ret;

	val = elm_slider_value_get(obj);
	sprintf(volume, "amixer -c 0 set PCM %d%s", (int)val, "\%\n");
	ret = write(fd, volume, strlen(volume));
	ret = system(volume);
	vol = (int)val;
}

void
pos_changed_start(void *data, Evas_Object *obj, void *event_info)
{
	if(timer1) ecore_timer_freeze(timer1);
}

void
pos_changed(void *data, Evas_Object *obj, void *event_info)
{
	double val;
	int ret;
	char pos[15];
	val = elm_slider_value_get(obj);
	pos_time = (int)val;
	sprintf(pos, "seek %d 2\n", pos_time);
	ret = write(fd, pos, strlen(pos));
	if (timer1) ecore_timer_thaw(timer1);
}

static int
file_select(const struct dirent *entry)
{
	if ((strcmp(entry->d_name, ".") == 0) ||
		(strcmp(entry->d_name, "..") == 0) || 
		(strncmp(entry->d_name,".",1) == 0))
						 return 0;
	else return 1;
}

void load_dir(Evas_Object *li, char *direc)
{
	int n, i;
    struct dirent **eps;
	char old_dir[255];
    strcpy(old_dir, direc);

	n = scandir (old_dir, &eps, file_select, alphasort);
    if (n > 0) {
	    //clear the list
		elm_genlist_clear(li);
	   	for (i = 0; i < n; ++i) {
			if(strcmp(old_dir, "/") == 0) sprintf(dir_gl_entry[i].path, "/%s", eps[i]->d_name);
			else sprintf(dir_gl_entry[i].path, "%s/%s", old_dir, eps[i]->d_name);
			dir_gl_entry[i].type = eps[i]->d_type;
			elm_genlist_item_append(li, &itc_dir_list, &dir_gl_entry[i], NULL, 
									ELM_GENLIST_ITEM_NONE, NULL, NULL);

		}
	}
    else return;
    free(eps);
	elm_genlist_item_selected_set(elm_genlist_first_item_get(li) ,1);
}

void up_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
    char tystr[255], ty[255];
    int i, flag=0;    
    Evas_Object *l = data;
    Elm_Genlist_Item *item = (Elm_Genlist_Item *)elm_genlist_selected_item_get(l);
    _dir *dir_gl = (_dir *)elm_genlist_item_data_get(item);
	char *path = dir_gl->path;
	
    if(strcmp(path, "/") == 0) strcpy(tystr, path);
    else {
        int n = strlen(path);
        strcpy(ty, path);
        for(i=n-1; i>0; i--) {
            if (ty[i] == '/') {
                if (flag == 1) break;
                else flag = 1;
            }
        }
        if (i == 0) strcpy(tystr, "/");
        else {
            strncpy(tystr, ty, i);
            tystr[i] = '\0';
        }
    }
	load_dir(l, tystr);
}

void home_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	Evas_Object *l = data;
	
	load_dir(l, home_dir);
}

void stop_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	int ret;	

	//stop playing video
	ret = write(fd, "pause\n", 6);
	//hide the window
	//evas_object_lower(inwin);
	if (timer1) ecore_timer_del(timer1);
	evas_object_hide(inwin);
	evas_object_resize(inwin, 1, 1);
}

void sel_file_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	int ret;
	char cmd[255], *ok;
	static Winlist wl;

	//get video file 
	Evas_Object *li = data;
	Elm_Genlist_Item *item = (Elm_Genlist_Item *)elm_genlist_selected_item_get(li);
	_dir *dir_gl = (_dir *)elm_genlist_item_data_get(item);
	char *path = dir_gl->path;
	//check file is a video file
	if ((ok = strstr(path, ".avi")) || (ok = strstr(path, ".flv"))) {	
		//close the file selection window
		wl.win = dir_browser_page_win;
		wl.li = dir_list;
		cancel_button_clicked (&wl, NULL, NULL);
		//open file with mplayer
		sprintf(cmd, "loadfile \"%s\"\n", path);
		ret = write(fd, cmd, strlen(cmd));
		//show inwin
		evas_object_show(inwin);
		play_state = PLAYING;
		timer1 = ecore_timer_add(1.0, timer_func, NULL);
		set_duration();
	}
}

void cancel_button_clicked(void *data, Evas_Object *obj, void *event_info)
{
	Winlist *wl = data;
	//clear the list
	if (wl->li) {
		elm_genlist_clear(wl->li);
	}
	//close the window
	evas_object_del(wl->win);
}

void dir_list_select(void *data, Evas_Object *obj, void *event_info)
{
	Evas_Object *li = data;
	Elm_Genlist_Item *item = (Elm_Genlist_Item *)elm_genlist_selected_item_get(li);
	_dir *dir_gl = (_dir *)elm_genlist_item_data_get(item);
	load_dir(li, dir_gl->path);
}

char *gl_label_get(const void *data, Evas_Object *obj, const char *part)
{
	char tystr[255], *ty;
	char *label = (char *)data;
	ty = strrchr(label, '/');
	ty ++;
	sprintf(tystr, "%s", ty);
	ty = strdup(tystr);
	return (ty);
}

Evas_Object *gl_icon_get(const void *data, Evas_Object *obj, const char *part)
{
	char buf[PATH_MAX];
	if (!strcmp(part, "elm.swallow.icon"))
	{
		Evas_Object *ic = elm_icon_add(obj);
		snprintf(buf, sizeof(buf), "%s/song.png", intone_dir);
		elm_icon_file_set(ic, buf, NULL);
		evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		return ic;
	}
	return NULL;
}

int gl_state_get(const void *data, Evas_Object *obj, const char *part)
{
   return 0;
}

void gl_del(const void *data, Evas_Object *obj)
{
}

void create_gui(Evas_Object *win)
{
	//playlist page - default view is playlist view
	Evas_Object *bg, *hbox, *play_button, *next_button, *stop_button, *vbox;
	
	//add background
	bg = elm_bg_add(win);
	elm_win_resize_object_add(win, bg);
	evas_object_size_hint_weight_set(bg, 1.0, 1.0);
	evas_object_show(bg);
	
	//add hbox
	hbox = elm_box_add(win);
	elm_box_horizontal_set(hbox, 1);
	elm_win_resize_object_add(win, hbox);
	evas_object_size_hint_weight_set(hbox, 1.0, 1.0);
	evas_object_show(hbox);

	//add inwin
	inwin = elm_win_inwin_add(win);
	elm_object_style_set(inwin, "default");
	evas_object_size_hint_weight_set(inwin, 1.0, 1.0);
	evas_object_size_hint_align_set(inwin, -1.0, -1.0);
	elm_box_pack_end(hbox, inwin);
	evas_object_show(inwin);
	
	//add vbox
	vbox = elm_box_add(win);
	evas_object_size_hint_weight_set(vbox, 0.0, 1.0);
	evas_object_size_hint_align_set(vbox, -1.0, -1.0);
	elm_box_pack_end(hbox, vbox);
	evas_object_show(vbox);

	//add total dur label label
	play_dur = elm_label_add(win);
	elm_label_label_set(play_dur, "0:00:00");
	elm_box_pack_end(vbox, play_dur);
	evas_object_show(play_dur);
	
	//add position slider to hbox
	pos_slider = elm_slider_add(win);
 	elm_slider_indicator_format_set(pos_slider, "%3.0f");
	evas_object_size_hint_weight_set(pos_slider, 1.0, 0.0);
	evas_object_size_hint_align_set(pos_slider, -1.0, 0.0);
	elm_slider_inverted_set(pos_slider, 1);
	elm_slider_horizontal_set(pos_slider, 0);
	elm_box_pack_end(vbox, pos_slider);
	elm_slider_min_max_set(pos_slider, 0, song_dur); //set to seconds
	elm_slider_value_set(pos_slider, (double)seek);
	evas_object_show(pos_slider);
	evas_object_smart_callback_add(pos_slider, "changed", pos_changed_start, NULL);
	evas_object_smart_callback_add(pos_slider, "delay,changed", pos_changed, NULL);
	
	//add cur pos label
	play_cur_pos = elm_label_add(win);
	elm_label_label_set(play_cur_pos, "0:00:00");
	elm_box_pack_end(vbox, play_cur_pos);
	evas_object_show(play_cur_pos);
	
	//add file button to the hbox
	next_button = elm_button_add(win);
	elm_button_label_set(next_button, "File");
	//evas_object_show(ic);
	evas_object_size_hint_weight_set(next_button, 1.0, 1.0);
	evas_object_size_hint_align_set(next_button, -1.0, -1.0);
	elm_box_pack_end(vbox, next_button);
	evas_object_show(next_button);
	evas_object_smart_callback_add(next_button, "clicked", create_dir_browser_page, list);

	//add stop button to the hbox
	stop_button = elm_button_add(win);
	/*ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "%s/play.png", intone_dir);
	elm_icon_file_set(ic, buf, NULL);
	evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
	elm_button_icon_set(stop_button, ic);
	evas_object_show(ic);*/
	evas_object_size_hint_weight_set(stop_button, 1.0, 1.0);
	evas_object_size_hint_align_set(stop_button, -1.0, -1.0);
	elm_button_label_set(stop_button, "Stop");
	elm_box_pack_end(vbox, stop_button);
	evas_object_show(stop_button);
	evas_object_smart_callback_add(stop_button, "clicked", stop_button_clicked, list);
	
	//add play button to the hbox
	play_button = elm_button_add(win);
	/*ic = elm_icon_add(win);
	snprintf(buf, sizeof(buf), "%s/play.png", intone_dir);
	elm_icon_file_set(ic, buf, NULL);
	evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
	elm_button_icon_set(play_button, ic);
	evas_object_show(ic);*/
	evas_object_size_hint_weight_set(play_button, 1.0, 1.0);
	evas_object_size_hint_align_set(play_button, -1.0, -1.0);
	elm_box_pack_end(vbox, play_button);
	elm_button_label_set(play_button, "Play");
	evas_object_show(play_button);
	evas_object_smart_callback_add(play_button, "clicked", play_button_clicked, list);

	// make window full screen
	evas_object_resize(win, 480, 600);
}

char *dir_gl_label_get(const void *data, Evas_Object *obj, const char *part)
{
	_dir *dir_gl = (_dir *)data;
	char tystr[255], *ty;
	char *label = dir_gl->path;
	
	ty = strrchr(label, '/');
	ty ++;
	sprintf(tystr, "%s", ty);
	ty = strdup(tystr);
	return (ty);
}

Evas_Object *dir_gl_icon_get(const void *data, Evas_Object *obj, const char *part)
{
	/*_dir *dir_gl = (_dir *)data;
	char buf[PATH_MAX], buf1[PATH_MAX];
	
	if (!strcmp(part, "elm.swallow.icon"))
	{
		Evas_Object *ic = elm_icon_add(obj);
		Evas_Object *ic1 = elm_icon_add(obj);
		
		snprintf(buf, sizeof(buf), "%s/folder.png", intone_dir);
		snprintf(buf1, sizeof(buf1), "%s/generic.png", intone_dir);
		elm_icon_file_set(ic, buf, NULL);
		elm_icon_file_set(ic1, buf1, NULL);
		evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		evas_object_size_hint_aspect_set(ic1, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		if (dir_gl->type == DT_DIR) return ic;
		else return ic1;
	}*/
	return NULL;
}

Eina_Bool dir_gl_state_get(const void *data, Evas_Object *obj, const char *part)
{
   return 0;
}

void dir_gl_del(const void *data, Evas_Object *obj)
{
}

void create_dir_browser_page(void *data, Evas_Object *obj, void *event_info)
{
	//TODO : Remember last browsed directory
	//Evas_Object *dir_browser_page_win;
	dir_browser_page_win = elm_win_add(NULL, "sel_direc", ELM_WIN_BASIC);
	elm_win_title_set(dir_browser_page_win, "Select File to Play");

	Evas_Object *bg, *vbox, *hbox, *hbox1, *up_button, *home_button, *enter_dir_button;
	Evas_Object *add_dir_button, *cancel_button;
	static Winlist wl;
	
	//add background
	bg = elm_bg_add(dir_browser_page_win);
	elm_win_resize_object_add(dir_browser_page_win, bg);
	evas_object_size_hint_weight_set(bg, 1.0, 1.0);
	evas_object_show(bg);

	//add vbox 3
	vbox = elm_box_add(dir_browser_page_win);
	elm_win_resize_object_add(dir_browser_page_win, vbox);
	evas_object_size_hint_weight_set(vbox, 1.0, 1.0);
	evas_object_show(vbox);

	// add button hbox
	hbox = elm_box_add(dir_browser_page_win);
	elm_box_horizontal_set(hbox, 1);
	evas_object_size_hint_weight_set(hbox, 1.0, 0.0);
	evas_object_size_hint_align_set(hbox, -1.0, 0.0);
	elm_box_pack_end(vbox, hbox);
	evas_object_show(hbox);

	//add up button to hbox
	up_button = elm_button_add(dir_browser_page_win);
	elm_button_label_set(up_button, "Up");
	evas_object_size_hint_weight_set(up_button, 1.0, 1.0);
	evas_object_size_hint_align_set(up_button, -1.0, -1.0);
	elm_box_pack_end(hbox, up_button);
	evas_object_show(up_button);

	//add home button to hbox
	home_button = elm_button_add(dir_browser_page_win);
	elm_button_label_set(home_button, "Home");
	evas_object_size_hint_weight_set(home_button, 1.0, 1.0);
	evas_object_size_hint_align_set(home_button, -1.0, -1.0);
	elm_box_pack_end(hbox, home_button);
	evas_object_show(home_button);

	//add enter dir button to hbox
	enter_dir_button = elm_button_add(dir_browser_page_win);
	elm_button_label_set(enter_dir_button, "Enter Dir");
	evas_object_size_hint_weight_set(enter_dir_button, 1.0, 1.0);
	evas_object_size_hint_align_set(enter_dir_button, -1.0, -1.0);
	elm_box_pack_end(hbox, enter_dir_button);
	evas_object_show(enter_dir_button);

	//add list to vbox now
	dir_list = elm_genlist_add(dir_browser_page_win);
	elm_object_scale_set(dir_list, 1.0);
	evas_object_size_hint_weight_set(dir_list, 1.0, 1.0);
	evas_object_size_hint_align_set(dir_list, -1.0, -1.0);
	elm_box_pack_end(vbox, dir_list);
	evas_object_show(dir_list);
	evas_object_smart_callback_add(dir_list, "clicked", dir_list_select, dir_list);
	evas_object_smart_callback_add(up_button, "clicked", up_button_clicked, dir_list);
	evas_object_smart_callback_add(home_button, "clicked", home_button_clicked, dir_list);   
	evas_object_smart_callback_add(enter_dir_button, "clicked", dir_list_select, dir_list); 

	//genlist class defs
	itc_dir_list.item_style     		= "default";
	itc_dir_list.func.label_get 		= dir_gl_label_get;
	itc_dir_list.func.icon_get  		= dir_gl_icon_get;
	itc_dir_list.func.state_get 		= dir_gl_state_get;
	itc_dir_list.func.del       		= dir_gl_del;
	
	// add button hbox1
	hbox1 = elm_box_add(dir_browser_page_win);
	elm_box_horizontal_set(hbox1, 1);
	evas_object_size_hint_weight_set(hbox1, 1.0, 0.0);
	evas_object_size_hint_align_set(hbox1, -1.0, 0.0);
	elm_box_pack_end(vbox, hbox1);
	evas_object_show(hbox1);

	//add add_dir button to hbox1
	add_dir_button = elm_button_add(dir_browser_page_win);
	elm_button_label_set(add_dir_button, "Sel File");
	evas_object_size_hint_weight_set(add_dir_button, 1.0, 1.0);
	evas_object_size_hint_align_set(add_dir_button, -1.0, -1.0);
	elm_box_pack_end(hbox1, add_dir_button);
	evas_object_show(add_dir_button);
	evas_object_smart_callback_add(add_dir_button, "clicked", sel_file_button_clicked, dir_list);

	//add cancel button to hbox1
	cancel_button = elm_button_add(dir_browser_page_win);
	elm_button_label_set(cancel_button, "Done");
	evas_object_size_hint_weight_set(cancel_button, 1.0, 1.0);
	evas_object_size_hint_align_set(cancel_button, -1.0, -1.0);
	elm_box_pack_end(hbox1, cancel_button);
	evas_object_show(cancel_button);
	wl.win = dir_browser_page_win;
	wl.li = dir_list;
	evas_object_smart_callback_add(cancel_button, "clicked", cancel_button_clicked, &wl);
	
	//set delete callback for this window
	evas_object_smart_callback_add(dir_browser_page_win, "delete-request", cancel_button_clicked, &wl);

	//load directory data
	load_dir(dir_list, "/");

	evas_object_resize(dir_browser_page_win, 480, 600);
	evas_object_show(dir_browser_page_win);
}
